<?php
	class Base_validation_controller extends Controller
	{
		public function __construct()
		{
			parent::__construct();
		}
		
		
		public function valid_phone_check($str)
		{
			if (preg_match('/^\(?[0-9]{0,3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/', $str))
            {
            	return TRUE;
            }
            else
            {
                $this->validation->set_message('valid_phone_check', 'The %s field must be a valid phone number.');
				return FALSE;
            }
		}
		
		
		public function valid_zip_code_check($str)
		{
			if(preg_match('/^\w{0,8}([\-]\w{0,4})?$/', $str))
			{
				return TRUE;
			}
			else
			{
				$this->validation->set_message('valid_zip_code_check', 'The %s field must be a valid zip code.');
				return FALSE;	
			}
		}
		
		
		public function valid_date_check($str)
	    {
			if(preg_match('/^\d{1,2}\/\d{1,2}\/\d{4}$/', $str))
			{
			  	$arr = explode("/", $str);       // splitting the array
	            $yyyy = $arr[2];            	 // year
	            $mm = $arr[0];              	 // month
	            $dd = $arr[1];              	 // day
	            if(is_numeric($yyyy) && is_numeric($mm) && is_numeric($dd))
				{
					if( checkdate($mm, $dd, $yyyy) )
					{
						return TRUE;
					}
					else
					{
						$this->validation->set_message('valid_date_check', 'The %s field must be in format: mm/dd/yyyy.');
						return FALSE;
					}
	            }
				else
				{
					$this->validation->set_message('valid_date_check', 'The %s field must be in format: mm/dd/yyyy.');
					return FALSE;
	            }
			} 
			else
			{
				$this->validation->set_message('valid_date_check', 'The %s field must be in format: mm/dd/yyyy.');
				return FALSE;
			}
	    }
		
		
		public function valid_user_name_check($str)
		{
			$checkUserName = $this->db->query('
												SELECT		p.person_id
												FROM		person p
												WHERE		p.person_name = ' . $this->db->escape($str) . '
											 ');
			
			if ( $checkUserName->num_rows() == 0 )
            {
            	return TRUE;
            }
            else
            {
                $this->validation->set_message('valid_user_name_check', 'The user name you entered is already taken, please try another name.');
				return FALSE;
            }
		}
		
		
		public function valid_dollar_check($str)
		{
			if( $str > 0 )
			{
				return TRUE;
			}
			else
			{
				$this->validation->set_message('valid_dollar_check', 'The %s field must be greater then $0.');
				return FALSE;
			}
		}
	}
?>